package kr.saeildamil.androidstudy.ch12;

import java.util.ArrayList;

import kr.saeildamil.androiddforstudy.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListOfViews extends Activity {
	
	ArrayList<ListItem> arItem;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listview_test);
		
		arItem = new ArrayList<ListItem>();
		arItem.add(new ListItem(0, "제목", "입력내용", "", 0));
		arItem.add(new ListItem(1, "", "", "Push", R.drawable.ic_launcher));
		arItem.add(new ListItem(0, "이름을 입력해주세요.", "당신의 이름", "", 0));
		arItem.add(new ListItem(0, "나이를 입력해주세요", "당신의 나이는", "", 0));
		arItem.add(new ListItem(1, "", "", "클릭하세요.", R.drawable.ic_launcher));
		arItem.add(new ListItem(1, "", "", "업로드", R.drawable.ic_launcher));
		arItem.add(new ListItem(0, "주소를 입력해주세요.", "당신이 사는 곳은", "", 0));
		arItem.add(new ListItem(1, "", "", "클릭하세요.", R.drawable.ic_launcher));
		arItem.add(new ListItem(1, "", "", "업로드", R.drawable.ic_launcher));
		
		MultiAdapter MyAdapter = new MultiAdapter(this,arItem);
		
		ListView MyList;
		MyList = (ListView) findViewById(R.id.ch12_listviewtest_list);
		MyList.setAdapter(MyAdapter);
	}
}

 /*class ListItem1 {
	public ListItem1(int aType, String aText, String aEditText, String aBtnText,int aIconRes) {
		Type = aType;
		Text = aText;
		EditText = aEditText;
		BtnText = aBtnText;
		IconRes = aIconRes;
		
	}
	int Type;
	String Text;
	String EditText;
	String BtnText;
	int IconRes;
}*/

 class MultiAdapter extends BaseAdapter{
	 LayoutInflater inflater;
	 ArrayList<ListItem> arSrc;
	 
	 public MultiAdapter(Context context, ArrayList<ListItem> arItem){
		 inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		 arSrc = arItem;
	 }
	 
	@Override
	public int getCount() {
		return arSrc.size();
	}
	
	@Override
	public Object getItem(int position) {
		return arSrc.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position){
		return arSrc.get(position).Type;
	}
	
	public int getViewTypeCount(){
		return 2;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null){
			int res1 = 0;
			switch (arSrc.get(position).Type) {
			case 0:
				res1 = R.layout.ch12_textedit;
				break;
			case 1:
				res1 = R.layout.ch12_btnicon;
				break;
			}
			convertView = inflater.inflate(res1, parent, false);
		}
		switch (arSrc.get(position).Type) {
		case 0:
			TextView txt = (TextView) convertView.findViewById(R.id.ch12_textedit_tv);
			txt.setText(arSrc.get(position).Text);
			EditText edit = (EditText) convertView.findViewById(R.id.ch12_textedit_edit);
			edit.setText(arSrc.get(position).EditText);
			break;
		case 1:
			Button btn = (Button) convertView.findViewById(R.id.ch12_textedit_btn);
			btn.setText(arSrc.get(position).BtnText);
			ImageView img = (ImageView) convertView.findViewById(R.id.ch12_textedit_img);
			img.setImageResource(arSrc.get(position).IconRes);
			break;
		}
		return convertView;
	}
	 
 }
